import {
  Controller,
  Get,
  Post,
  Body,
  Put,
  Param,
  Delete,
  UseGuards,
  Query,
} from '@nestjs/common';
import { SkuService } from './sku.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { AddressService } from '../address/address.service';

@Controller('sku')
@UseGuards(JwtAuthGuard)
export class SkuController {
  constructor(
    private readonly skuService: SkuService,
    private readonly addressService: AddressService,
  ) {}

  @Post()
  create(
    @Body()
    createSkuDto: {
      title: string;
      price?: number;
      photoCount: number;
      page?: string;
    },
  ) {
    return this.skuService.create(createSkuDto);
  }

  @Get()
  findAll() {
    return this.skuService.findAll();
  }

  @Get('address')
  async findAllWithAddress(@Query('phone') phone: string) {
    const [skuList, address] = await Promise.all([
      this.skuService.findAll(),
      this.addressService.getAddress(phone),
    ]);
    return {
      skuList,
      address: {
        province: address.province,
        city: address.city,
        district: address.district,
        details: address.details,
        phone: address.phone,
      },
    };
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.skuService.findOne(+id);
  }

  @Put(':id')
  update(
    @Param('id') id: string,
    @Body() updateSkuDto: { title?: string; price?: number },
  ) {
    return this.skuService.update(+id, updateSkuDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.skuService.remove(+id);
  }
}
